﻿using System;
using System.Collections.Generic;
using URandom = Unity.Mathematics.Random;
using UnityEngine;

[Serializable]
public class PlatformBuilder
{

    [SerializeField]
    Transform parentGameObject;

    [SerializeField]
    List<Transform> safePlatforms;
    [SerializeField]
    List<Transform> dangerPlatforms;

    // 平台的出生点
    [SerializeField]
    Vector2[] platformBrithPoints = new Vector2[] {
        new Vector2{ x=-2.85f, y=-12.77f },
        new Vector2{ x=-1f, y=-12.77f},
        new Vector2{ x=0.8f, y=-12.77f},
        new Vector2{ x=2.7f, y=-12.77f}
    };

    [SerializeField]
    float previousDistance = 1f;      // 与上一个平台的距离

    [SerializeField]
    int detectTimes = 4;

    int tmpDetectTimes;
    bool hasCreateSafePlatform;

    Transform prePlatform;     // 上一个平台的坐标，为了避免两次生成在同一个位置

    bool initialized;
    URandom random = new URandom();

    

    public void Build()
    {
        if (safePlatforms == null || safePlatforms.Count == 0)
        {
            throw new ArgumentException("Argument safePlatforms is null or count is 0");
        }
        if (dangerPlatforms == null || dangerPlatforms.Count == 0)
        {
            throw new ArgumentException("Argument dangerPlatforms is null or count is 0");
        }

        Init();

        Vector2 platformPositon = FindPosition();
        if (Vector2.negativeInfinity.Equals(platformPositon))
        {
            return;
        }
        Debug.Log($"Instantiate platform  x:{platformPositon.x}, y:{platformPositon.y}");
        prePlatform = UnityEngine.Object.Instantiate(FindPlatform(), platformPositon, Quaternion.identity, parentGameObject);
    }


    private void Init()
    {
        if (initialized)
        {
            return;
        }
        random.InitState();

        initialized = true;
    }


    // 产生平台坐标，若失败，返回 Vector2.negativeInfinity
    private Vector2 FindPosition()
    {
        int index;
        Vector2 point;
        do
        {
            index = (int)random.NextUInt((uint)platformBrithPoints.Length);
            point = platformBrithPoints[index];
            if (prePlatform == null)
            {
                break;
            }
            // 检测上一个平台与新生成的平台距离，未达到设定的距离时不创建新平台
            if (previousDistance > (prePlatform.position.y - point.y))
            {
                return Vector2.negativeInfinity;
            }
        } while (prePlatform.position.x == point.x);

        return point;
    }


    private Transform FindPlatform()
    {
        if (tmpDetectTimes <= 0)
        {
            hasCreateSafePlatform = false;
            tmpDetectTimes = detectTimes;
        }
        tmpDetectTimes--;

        if (tmpDetectTimes == 0 && !hasCreateSafePlatform)
        {
            return FindSafePlatform();
        }

        if (random.NextFloat(0f, 1f) < 0.5f)
        {
            hasCreateSafePlatform = true;
            return FindSafePlatform();
        }
        return FindDangerPlatform();
    }

    private Transform FindDangerPlatform()
    {
        int index = (int)random.NextUInt((uint)safePlatforms.Count);
        return safePlatforms[index];
    }

    private Transform FindSafePlatform()
    {
        int index = (int)random.NextUInt((uint)dangerPlatforms.Count);
        return dangerPlatforms[index];
    }
}
